# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  session :off  # Session off by default
  
  helper :all # include all helpers, all the time

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  # protect_from_forgery # :secret => '18a009eca64edfaed3f52ebe70b68293'
  
  # See ActionController::Base for details 
  # Uncomment this to filter the contents of submitted sensitive data parameters
  # from your application log (in this case, all fields with names like "password"). 
  filter_parameter_logging :password
  
  before_filter :get_recent_comments, :init_nav, :prepare_page_number
  
  def initialize
    @nav = "nav"
  end

  def render_optional_error_file(status_code)
    status = interpret_status(status_code)
    @status_code = status[0,3]
    @status_message = status[4, status.length]
    render :template => "error/default"
  end
  # alias_method :rescue_action_locally, :rescue_action_in_public
  
  protected
    def admin?
      session[:admin] == 'eusden'
    end
    
    def set_admin
      session[:admin] = 'eusden'
    end
    
    def get_recent_comments
      @recent_comments = Comment.get_items
    end
    
    def init_nav
      @nav_sections ||= []
      @nav_sections << ["air_quality", {}]
      @nav_sections << ["current_time", {}]
    end
    
    def prepare_page_number
      @page = params[:page].to_i > 0 ? params[:page].to_i : 1
    end

    def set_cookie(key, value, expires_in = self.class.session_options[:expires])
      cookie = {
        :value => value,
        :domain => self.class.session_options[:session_domain]
      }
      if value.blank?
        cookie[:expires] = 1.year.ago
      elsif expires_in
        cookie[:expires] = Time.now + expires_in
      end
      cookies[key] = cookie
    end
end
